var myChart1 = echarts.init(document.getElementById("MilitarySecondGrade"));
var myChart2 = echarts.init(document.getElementById("CivilianGoods"));
var myChart3 = echarts.init(document.getElementById("PreResearch"));
var myChart4 = echarts.init(document.getElementById("Portrait"));

$(function () {
  chenbenzucheng = 1;
  initchenbenzuchengData();
})

function initchenbenzuchengData() {
  const username = localStorage.getItem("user");
  const currYear = localStorage.getItem("year");
  const currMon = localStorage.getItem("month");
  const currDept = localStorage.getItem("dept");

  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 60101,
    chartNo: 11,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    // console.log(chartData)
    $(".title1").text(chartData[0].key01);
    $(".title2").text(chartData[1].key01);
    $(".title3").text(chartData[2].key01);
    $(".title4").text(chartData[3].key01);

    $(".junerpintotolNum").text(pricefmt((chartData[0].key02 / 10000).toFixed(2)));
    $(".minpintotolNum").text(pricefmt((chartData[1].key02 / 10000).toFixed(2)));
    $(".yuyantotolNum").text(pricefmt((chartData[2].key02 / 10000).toFixed(2)));
    $(".zongxiangtotolNum").text(pricefmt((chartData[3].key02 / 10000).toFixed(2)));
  });

  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 60101,
    chartNo: 7,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    // console.log(chartData)
    $(".name1").text(chartData[0].key01);
    $(".pre1").text(pricefmt((chartData[0].key03 * 100).toFixed(2)));
    $(".name2").text(chartData[1].key01);
    $(".pre2").text(pricefmt((chartData[1].key03 * 100).toFixed(2)));
    getjunChart(chartData) //军二品费用
  });

  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 60101,
    chartNo: 8,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    console.log(chartData, 'min')
    $(".name3").text(chartData[0].key01);
    $(".pre3").text(pricefmt((chartData[0].key03 * 100).toFixed(2)));
    $(".name4").text(chartData[1].key01);
    $(".pre4").text(pricefmt((chartData[1].key03 * 100).toFixed(2)));
    getminChart(chartData) //民品费用
  });

  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 60101,
    chartNo: 9,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    console.log(chartData, 'yu')
    $(".name5").text(chartData[0].key01);
    $(".pre5").text(pricefmt((chartData[0].key03 * 100).toFixed(2)));
    $(".name6").text(chartData[1].key01);
    $(".pre6").text(pricefmt((chartData[1].key03 * 100).toFixed(2)));
    getyuyanChart(chartData) ////预研费用
  });

  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 60101,
    chartNo: 10,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    console.log(chartData, 'zong')


    $(".name7").text(chartData[0].key01);
    $(".pre7").text(pricefmt((chartData[0].key03 * 100).toFixed(2)));
    $(".name8").text(chartData[1].key01);
    $(".pre8").text(pricefmt((chartData[1].key03 * 100).toFixed(2)));
    $(".name9").text(chartData[2].key01);
    $(".pre9").text(pricefmt((chartData[2].key03 * 100).toFixed(2)));
    getzongxiangChart(chartData); //纵向费用
  });
}
const colors =['#165DFF', '#50CD89', '#722ED1','#F7BA1E','#0DCAF0','#F99057','#C9CDD4']
function getjunChart(data) {
  console.log(data, '军二品费用')
  let junChartData = []
  data.forEach((item) => {
    console.log(item, 'iii')
    let obj = {
      value: pricefmt((item.key02 / 10000).toFixed(2)),
      name: item.key01
    }
    junChartData.push(obj)
  })

  let junChart = {
    tooltip: {
      trigger: 'item'
    },
    series: [{
      type: 'pie',
      radius: '94%',
      avoidLabelOverlap: false,
      itemStyle: {
        borderColor: '#fff',
        borderWidth: 2
      },
      label: {
        normal: {
          show: false,
        }
      },
      labelLine: {
        normal: {
          show: false
        }
      },
      color: colors,
      data: junChartData,
      emphasis: {
        itemStyle: {
          shadowBlur: 2,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
        }
      }
    }]
  };
  myChart1.setOption(junChart);
  window.addEventListener("resize", function () {
    myChart1.resize();
  });
  $("div").resize(function () {
    myChart1.resize();
  });
}

function getminChart(data) {

  console.log(data, '民品费用') //民品费用
  let minChartData = []
  data.forEach((item) => {
    console.log(item, 'iii')
    let obj = {
      value: pricefmt((item.key02 / 10000).toFixed(2)),
      name: item.key01
    }
    minChartData.push(obj)
  })
  let minChart = {
    tooltip: {
      trigger: 'item'
    },
    series: [{
      type: 'pie',
      radius: '94%',
      avoidLabelOverlap: false,
      itemStyle: {
        borderColor: '#fff',
        borderWidth: 2
      },
      label: {
        normal: {
          show: false,
        }
      },
      labelLine: {
        normal: {
          show: false
        }
      },
      color: colors,
      data: minChartData,
      emphasis: {
        itemStyle: {
          shadowBlur: 2,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
        }
      }
    }]
  };
  myChart2.setOption(minChart);
  window.addEventListener("resize", function () {
    myChart2.resize();
  });
  $("div").resize(function () {
    myChart2.resize();
  });
}

function getyuyanChart(data) {
  console.log(data, 'yiuyan')
  let yuyanChartData = []
  data.forEach((item) => {
    console.log(item, 'iii')
    let obj = {
      value: pricefmt((item.key02 / 10000).toFixed(2)),
      name: item.key01
    }
    yuyanChartData.push(obj)
  })

  let yuyanChart = {
    tooltip: {
      trigger: 'item'
    },
    series: [{
      type: 'pie',
      radius: '94%',
      avoidLabelOverlap: false,
      itemStyle: {
        borderColor: '#fff',
        borderWidth: 2
      },
      label: {
        normal: {
          show: false,
        }
      },
      labelLine: {
        normal: {
          show: false
        }
      },
      color: colors,
      data: yuyanChartData,
      emphasis: {
        itemStyle: {
          shadowBlur: 2,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
        }
      }
    }]
  };
  myChart3.setOption(yuyanChart);
  window.addEventListener("resize", function () {
    myChart3.resize();
  });
  $("div").resize(function () {
    myChart3.resize();
  });
}

function getzongxiangChart(data) {
  let zongxiangChartData = []
  data.forEach((item) => {
    console.log(item, 'iii')
    let obj = {
      value: pricefmt((item.key02 / 10000).toFixed(2)),
      name: item.key01
    }
    zongxiangChartData.push(obj)
  })

  let zongxiangChart = {
    tooltip: {
      trigger: 'item'
    },
    series: [{
      type: 'pie',
      radius: '94%',
      avoidLabelOverlap: false,
      itemStyle: {
        borderColor: '#fff',
        borderWidth: 2
      },
      label: {
        normal: {
          show: false,
        }
      },
      labelLine: {
        normal: {
          show: false
        }
      },
      color: colors,
      data: zongxiangChartData,
      emphasis: {
        itemStyle: {
          shadowBlur: 2,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
        }
      }
    }]
  };
  myChart4.setOption(zongxiangChart);
  window.addEventListener("resize", function () {
    myChart4.resize();
  });
  $("div").resize(function () {
    myChart4.resize();
  });
}